﻿using SIG.Basic.Extensions;
using SIG.Data.Repositories.Case;
using SIG.Infrastructure.Cache;
using SIG.Infrastructure.Configs;
using SIG.Infrastructure.Logging;
using SIG.Model.Case;
using SIG.Resources.Admin;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace SIG.Service.Case
{
    public class SolutionService : EntityService<Solution>, ISolutionService
    {

        private readonly ISolutionRepository _clientRepository;
        private readonly ILoggingService _loggingService;
        private readonly ICacheService _cacheService;

        public SolutionService(ISolutionRepository clientRepository, ILoggingService loggingService, ICacheService cacheService)
            : base(clientRepository, cacheService, loggingService)
        {
            _clientRepository = clientRepository;
            _loggingService = loggingService;
            _cacheService = cacheService;
        }



        public List<Solution> GetPagedElements(int pageIndex, int pageSize, string keyword, out int totalCount)
        {

            //get list count
            var keyCount = $"{EntityNames.Solution}_ListCount_{keyword}";

            var totalIQuery = _clientRepository.GetAllElements().Where(g => g.Active);
            if (!string.IsNullOrEmpty(keyword))
                totalIQuery = totalIQuery.Where(g => g.Title.Contains(keyword));

            if (SettingsManager.Case.EnableCaching)
            {
                if (_cacheService.IsSet(keyCount))
                {
                    totalCount = (int)_cacheService.Get(keyCount);
                }
                else
                {
                    totalCount = totalIQuery.Count();
                    _cacheService.Set(keyCount, totalCount, SettingsManager.Case.CacheDuration);
                }
            }
            else
            {
                totalCount = totalIQuery.Count();
            }


            //get list
            var key = $"{EntityNames.Solution}_List_{pageIndex}_{pageSize}_{keyword}";

            List<Solution> categories;
            Expression<Func<Solution, bool>> filter = g => g.Active;
            Expression<Func<Solution, bool>> filterByKeyword = g => g.Title.Contains(keyword);


            if (!string.IsNullOrEmpty(keyword))
                filter = filter.AndAlso(filterByKeyword);


            if (SettingsManager.Case.EnableCaching)
            {
                if (_cacheService.IsSet(key))
                {
                    categories = (List<Solution>)_cacheService.Get(key);

                }
                else
                {
                    categories = _clientRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), filter, false).ToList();
                    _cacheService.Set(key, categories, SettingsManager.Case.CacheDuration);
                }
            }
            else
            {
                categories = _clientRepository.GetPagedElements(pageIndex, pageSize, (c => c.Importance), filter, false).ToList();
            }

            return categories;

        }

    }
}
